package com.dongline.test.project1;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.NullWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.Mapper;
import org.apache.hadoop.mapreduce.Reducer;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;

import java.io.IOException;

public class MyMapper extends Mapper<LongWritable, Text,Text, Text> {


    Text text=new Text();
    Text t=new Text("tmp");
    @Override
    protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {
        String line=value.toString();
        if(line!=null)
        {
            String [] strs=line.split("\\s+");
            if(strs.length>3)
            {
                if(strs[3].equals("c.r.w.LogCostFilter")&&strs[7].split("\\.").length<2)
                {

                    text.set(line);
                    context.write(t,text);

                }
            }
        }



    }



    public  static class MyReducer extends Reducer<Text,Text,Text,NullWritable>
    {
        Text text=new Text();
        @Override
        protected void reduce(Text key, Iterable<Text> values, Context context) throws IOException, InterruptedException {
            for (Text tmp:values)
            {
                text.set(tmp);
                context.write(text,NullWritable.get());
            }
        }
    }

    public static void main(String[] args) throws IOException, ClassNotFoundException, InterruptedException {

        Configuration configuration = new Configuration();


        Job job=Job.getInstance(configuration);

        ///这句话很重要必须加上
        job.setJarByClass(MyMapper.class);


        job.setMapperClass(MyMapper.class);
        job.setReducerClass(MyReducer.class);

//        job.setNumReduceTasks(1);
//
        job.setMapOutputKeyClass(Text.class);
        job.setMapOutputValueClass(Text.class);



        job.setOutputKeyClass(Text.class);
        job.setOutputValueClass(NullWritable.class);




        FileInputFormat.setInputPaths(job,new Path(args[0]));
        FileOutputFormat.setOutputPath(job,new Path(args[1]));



//        job.setCombinerClass();可以设置一个Combiner，功能类似于reducer，但是是运行在maptask上的。maptask自己数据进行分区排序之前调用一次。分区排序后调用一次。业务场景



        boolean re= job.waitForCompletion(true);

        System.exit(re?0:1);
    }

}
